package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.Course;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.entity.CoursePriceSchool;
import com.xmy.cultivate.entity.CoursePricing;
import com.xmy.cultivate.entity.views.StudentCourse;
import com.xmy.cultivate.entity.views.StudentCourseGrade;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程表 Mapper 接口
 * </p>
 *
 * @author min
 * @since 2022-07-26
 */
@Mapper
public interface CourseMapper extends BaseMapper<Course> {

    @Select("select a.*,b.`name` as courseTypeName, getOpenClassCount(a.id) as openGradeCount from course as a left join course_type b on a.course_type_id = b.id ${ew.customSqlSegment}")
    /*@Results({
            @Result(
                    column = "id",property = "id"
            ),
            @Result(
                    property = "coursePricingList",column = "id",many = @Many(select = "coursePricingList")
            )
    })*/
    public abstract IPage<Course> checkCourse(IPage<?> page, @Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select a.id,a.name,a.charge_mode,a.teaching_mode,a.subjects_id,a.year_class_id,a.quarter_num,b.`name` as type_name,getOpenClassCount(a.id) as openGradeCount from course as a left join course_type b on a.course_type_id = b.id where a.id = #{id} and a.deleted = 0")
    public abstract Course checkCourseForId(Long id);


    @Select("SELECT id,count,price,count_unit,serve_price,materials_price,all_price FROM course_pricing WHERE course_id = #{id} and deleted = 0")
    public abstract List<CoursePricing> coursePricingList(@Param("id") Long id);


    @Select("SELECT id,count,price,count_unit,serve_price,materials_price,all_price FROM course_pricing WHERE  (school_id = #{school_id} or school_id = 0 ) and course_id = #{id} and deleted = 0 ")
    public abstract List<CoursePricing> coursePricingForSchoolIdList(@Param("id") Long id,@Param("school_id") Long school_id);


    @Select("SELECT *,getOpenClassCount(id) as openGradeCount,getCourseTypeName(course_type_id) as courseTypeName FROM course ${ew.customSqlSegment}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(
                    property = "priceSchoolList",
                    column = "id",
                    many=@Many(select = "coursePriceSchool")
            )
    })
    public abstract IPage<Course> findAll(IPage<?> page,@Param(Constants.WRAPPER) Wrapper wrapper);


    @Select("SELECT *,getSchoolName(school_id) as school_name FROM course_price_school where course_id = #{course_id} and deleted=0")
    @Results({
            @Result(
                    column = "id",property = "id"
            ),
            @Result(
                    property = "coursePricingList",
                    column = "id",
                    many = @Many(select = "coursePricing")
            )
    })
    public abstract List<CoursePriceSchool> coursePriceSchool(@Param("id") Long course_id);

    @Select("SELECT * FROM course_pricing where price_school_id = #{id} and deleted=0")
    public abstract List<CoursePricing> coursePricing(@Param("id") Long id);


    @Select("SELECT * FROM course WHERE id = #{id}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(
                    property = "coursePricingList",
                    column = "id",
                    many=@Many(select = "coursePriceSchool")
            ),
            @Result(
                    property = "appointSchoolList",
                    javaType = List.class,
                    column = "id",
                    many=@Many(select = "getAppoidSchoolId")
            )
    })
    public abstract Course findOne(@Param("id") Long id);

    @Select("SELECT school_id FROM appoint_school where for_id = #{course_id} and deleted = 0")
    public abstract List<String> getAppoidSchoolId(@Param("course_id") Long course_id);

    @Select("select * from student_course_view ${ew.customSqlSegment}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(
                    property = "gradeList",
                    column = "id",
                    many = @Many(select = "com.xmy.cultivate.mapper.CourseMapper.getGradeForOrderDetail")
            )
    })
    public abstract IPage<StudentCourse> studentCourse(IPage<?> page,@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("SELECT a.reading_status,a.grade_id,b.`name` as gradeName,b.teacher_id,getStaffNameForId(b.teacher_id) as teacherName,b.open_date,a.use_course_count FROM student_grade as a LEFT JOIN grade as b on a.grade_id = b.id WHERE a.status<>3 and order_detail_id = #{orderDetailId}")
    public abstract List<StudentCourseGrade> getGradeForOrderDetail(@Param("orderDetailId") Long orderDetailId);
}
